Components.utils.import("resource://telescope/modules/Preferences.js"); 

function onOK()
{
    var portStr = document.getElementById('port').value;
    var portInt = parseInt(portStr);
	var authForRead = document.getElementById('authForRead').checked;
	var authForWrite = document.getElementById('authForWrite').checked;
    var password = document.getElementById('password').value;
	
	var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                        .getService(Components.interfaces.nsIPromptService);
						
    if (portInt < 0 || portInt > 65335)
    {
        prompts.alert(window, "Invalid Port", "The specified port is invalid");
        return false;
	}
	if ((authForRead || authForWrite) && password == '')
	{
        prompts.alert(window, "No Password", "Please set a password");
        return false;
	}
	
	Preferences.set("extensions.telescope.port",portInt);
	Preferences.set("extensions.telescope.authForRead",authForRead);
	Preferences.set("extensions.telescope.authForWrite",authForWrite);
	Preferences.set("extensions.telescope.password",password);
	return true;
}

function onLoad()
{
	var port = Preferences.get("extensions.telescope.port",3690);
	var hostName = Components.classes["@mozilla.org/network/dns-service;1"].getService(Components.interfaces.nsIDNSService).myHostName;
	var hostNameLink = 'http://' + hostName + ':' + port + '/';
	
	document.getElementById('serverLink').href = hostNameLink;
	document.getElementById('serverLink').value = hostNameLink;
    document.getElementById('port').value = port;
    document.getElementById('authForRead').checked = Preferences.get("extensions.telescope.authForRead",false);
    document.getElementById('authForWrite').checked = Preferences.get("extensions.telescope.authForWrite",false);
    document.getElementById('password').value = Preferences.get("extensions.telescope.password","");
}